"""
_*_ : utf-8 _*_ 
@Time: 2023/11/18 21:07
@Author: LWK
@File: 083_requests
@Project: demo01
"""
import requests
from bs4 import BeautifulSoup

# 1、隐藏域
# 2、验证码

# 登录界面URL
url = 'https://so.gushiwen.cn/user/login.aspx?from=http://so.gushiwen.cn/user/collect.aspx'

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.76'
}

res = requests.get(url=url, headers=headers)
context = res.text

# 解析网页源码，获取 __VIEWSTATE __VIEWSTATEGENERATOR
soup = BeautifulSoup(context, 'lxml')
__VIEWSTATE = soup.select('#__VIEWSTATE')[0].attrs.get('value')
__VIEWSTATEGENERATOR = soup.select('#__VIEWSTATEGENERATOR')[0].attrs.get('value')

# 获取验证码图片
code = 'https://so.gushiwen.cn' + soup.select('#imgCode')[0].attrs.get('src')

# 有坑，相当于再次访问验证码地址，验证码会变化
# import urllib.request
# urllib.request.urlretrieve(url=code, filename='../temp/code.jpg')

session = requests.session()
# 获取验证码内容
code_content = session.get(code).content
# 二进制保存图片
with open('../temp/code.jpg', 'wb') as fp:
    fp.write(code_content)

code_inp = input('请输入验证码')

login_url = 'https://so.gushiwen.cn/user/login.aspx?from=http%3a%2f%2fso.gushiwen.cn%2fuser%2fcollect.aspx'

data_post = {
    '__VIEWSTATE': __VIEWSTATE,
    '__VIEWSTATEGENERATOR': __VIEWSTATEGENERATOR,
    'from': 'http://so.gushiwen.cn/user/collect.aspx',
    'email': '178440531@qq.com',
    'pwd': 'Aa1111',
    'code': code_inp,
    'denglu': '登录'
}

res_login = session.post(url=login_url, data=data_post, headers=headers)
content_post = res_login.text

with open('../temp/gushiwen.html', 'w', encoding='utf-8') as fp:
    fp.write(content_post)
